<template>
	<view class="page-fade-in" :style="{opacity: pageOpc}">
		<template v-if="pageOpc">
			<view class="content">
				<view class="coupon-box">
					<view class="title">优惠券：</view>
					<view class="coupon-item flexWrapNo" v-if="couponId" @tap="goPage('/pages/manage/couponList?isSelect=1')">
						<view class="coupon-left flexWrapNo">
							<text class="money">{{couponInfo.price}}</text>
							<text class="money-tip">元</text>
						</view>
						<view class="coupon-line"></view>
						<view class="coupon-right flexWarpColumn">
							<text class="coupon-name">{{couponInfo.title}}</text>
							<text v-if="couponInfo.valid_timedata==1">有效期：永久</text>
							<text v-if="couponInfo.valid_timedata==2">有效期：{{couponInfo.start}} 至 {{couponInfo.end}}</text>
							<text v-if="couponInfo.valid_timedata==3">有效期：{{couponInfo.valid_configure}}天</text>
						</view>
					</view>
					<view class="select-box" v-else @tap="goPage('/pages/manage/couponList?isSelect=1')">
						<text class="select-text">+选择优惠券</text>
					</view>
				</view>
				<view class="info-item flexWrap">
					<view class="flexWrapNo" style="flex: 1;">
						<view>赠送数量：</view>
						<input placeholder="请输入数量" placeholder-class="placeholderclass" type="number" v-model="coupon_num"/>
					</view>
					<text>张/人</text>
				</view>
				<view class="info-item flexWrap" v-if="!user_id" @tap="goPage('/pages/manage/memberSearch?couponId='+couponId)">
					<view>选择会员：</view>
					<view class="flexWrapNo">
						<text style="color: #EEC162;">查找</text>
						<text class="icon-more"></text>
					</view>
				</view>
				<view class="select-text" v-if="!user_id">已选<text>{{selectList.length}}</text>人</view>
				<view class="user-box" v-if="!user_id">
					<view class="user-item flexWrapNo" v-for="(item,index) in selectList" :key="index">
						<image src="../../static/coupon/delete-icon.png" class="delete-icon" @tap="deleteUser(index)"></image>
						<view class="user-info flexWrapNo">
							<view class="name">{{item.nickname}}</view>
							<view>{{item.mobile}}</view>
						</view>
					</view>
				</view>
				<view class="btn-box">
					<view class="determine-btn" @tap="couponSend()">确定发放</view>
				</view>
			</view>
		</template>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageOpc: 0,
				couponInfo: {},
				selectList:[],
				couponId:"",
				coupon_num:"",
				user_id:""
			}
		},
		onReady() {
			setTimeout(() => {
				this.pageOpc = 1
			}, 100)
		},
		onUnload() {
			uni.removeStorageSync('selectList')
		},
		onShow() {
			this.selectList=uni.getStorageSync('selectList')
		},
		onLoad(ops) {
			this.couponId=ops.couponId||''
			this.user_id=ops.user_id
			if(this.couponId) this.getCouponInfo();
		},
		methods: {
			deleteUser(index){
				this.selectList.splice(index,1)
			},
			// 获取优惠券信息
			getCouponInfo(){
				this.$request.post(this.$api.couponInfo, {
					store_id:uni.getStorageSync('store_id'),
					coupon_id:this.couponId
				}, {
					token:true
				}).then(res => {
					let {
						data = {}
					} = res;
					if (res.code==1) {
						if(data.valid_timedata==2){
							let customTimeArr=data.valid_configure.split(' - ')
							let start=customTimeArr[0]
							let end=customTimeArr[1]
							data.start=this.$tools.formatDay(start)
							data.end=this.$tools.formatDay(end)
						}
						this.couponInfo=data
					}
				})
			},
			// 发放优惠券
			couponSend(){
				if(!this.coupon_num){
					this.$tools.showToast('请输入数量')
					return;
				}else if(!this.couponId){
					this.$tools.showToast('请选择优惠券')
					return;
				}else if(this.selectList.length==0&&!this.user_id){
					this.$tools.showToast('请选择会员')
					return;
				}
				let ids=[]
				if(this.user_id){
					ids.push(this.user_id)
				}else{
					for (let i in this.selectList) {
						ids.push(this.selectList[i].user_id)
					}
				}
				ids=ids.join(',')
				this.$request.post(this.$api.couponSend, {
					store_id:uni.getStorageSync('store_id'),
					coupon_id:this.couponId,
					user_ids:ids,
					coupon_num:this.coupon_num
				}, {
					token:true
				}).then(res => {
					let {
						data = {}
					} = res;
					if (res.code==1) {
						this.$tools.showToast('发放成功')
						uni.removeStorageSync('selectList')
						if(this.user_id){
							let pages = getCurrentPages(); //当前页
							let beforePage = pages[pages.length - 2]; //上个页面
							beforePage.$vm.freshList()
						}
						setTimeout(res=>{
							uni.navigateBack()
						},1000)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: 100upx;
	}

	.content {
		padding: 0 36upx;
	}

	.info-item {
		font-size: 28upx;
		font-family: PingFang SC;
		font-weight: 400;
		line-height: 40upx;
		color: #333333;
		padding: 30upx 0;
		border-bottom: 1upx solid #E3E3E3;

		picker {
			flex: 1;
		}

		.select-input {
			font-size: 28rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			color: #969696;
			flex: 1;
		}

		input {
			font-size: 28rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			color: #969696;
			flex: 1;
		}

		.icon-more {
			border-color: #969696;
		}

		.placeholderclass {
			font-size: 28rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			color: #969696;
		}
	}

	.coupon-box {
		.title {
			font-size: 28rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #6E6E6E;
			margin-bottom: 22rpx;
		}

		.select-box {
			height: 140upx;
			line-height: 140upx;
			border: 1rpx dashed #FFFFFF;
			border-radius: 20upx 20upx;
			text-align: center;

			.select-text {
				font-size: 28rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #FD8926;
			}
		}

		.coupon-item {
			margin-bottom: 20upx;
			height: 140upx;
			background: #7EBE6E;
			border-radius: 20upx 20upx;
			position: relative;

			.left-circular {
				width: 30upx;
				height: 30upx;
				border-radius: 50%;
				background: #040336;
				position: absolute;
				left: -15upx;
				bottom: 0;
				bottom: -15rpx;
			}

			.right-circular {
				width: 30upx;
				height: 30upx;
				border-radius: 50%;
				background: #040336;
				position: absolute;
				right: -15upx;
				bottom: -15rpx;
			}

			.coupon-left {
				width: 224upx;
				align-items: baseline;
				justify-content: center;

				.money {
					font-size: 52upx;
					font-family: PingFang SC;
					font-weight: 500;
					line-height: 100upx;
					color: #000000;
				}

				.money-tip {
					font-size: 24upx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #000000;
					margin-left: 20rpx;
				}
			}

			.coupon-line {
				width: 2upx;
				height: 110upx;
				background: #040336;
			}

			.coupon-right {
				font-size: 24upx;
				font-family: PingFang SC;
				font-weight: 400;
				line-height: 44upx;
				color: #000000;
				margin-left: 32upx;
				flex: 1;
				.coupon-name {
					font-size: 36upx;
					font-family: PingFang SC;
					font-weight: 600;
					line-height: 50upx;
					color: #000000;
				}
			}

		}
	}

	.select-text {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #6E6E6E;
		margin-top: 46rpx;

		text {
			font-size: 28rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #EEC162;
			margin: 0 18rpx;
		}
	}

	.user-box {
		margin-top: 36rpx;

		.user-item {
			margin-bottom: 38rpx;

			.delete-icon {
				width: 40rpx;
				height: 40rpx;
				margin-right: 22rpx;
			}

			.user-info {
				font-size: 28rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #969696;

				.name {
					margin-right: 36rpx;
				}
			}
		}
	}

	.btn-box {
		height: 120rpx;
		background: #FFFFFF;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		margin: auto;

		.determine-btn {
			font-size: 32rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #333;
			width: 322rpx;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background: #EEC162;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			margin: 20rpx auto;
		}
	}
</style>
